package com.asus.sun.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * Druid配置解析
 * @Configuration  配置类注解
 * @RefreshScope   config配置文件 配置自动刷新注解
 * @author asus
 * @create 2018-12-25
 **/
@Configuration
public class DruidConfig {

    @Value("${spring.datasource.url}")
    private String dbUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClassName;

    @Value("${spring.datasource.initialSize: 1}")
    private String initialSize;

    @Value("${spring.datasource.minIdle: 1}")
    private String minIdle;

    @Value("${spring.datasource.maxActive: 20}")
    private String maxActive;

    @Value("${spring.datasource.maxWait: 60000}")
    private String maxWait;

    @Value("${spring.datasource.timeBetweenEvictionRunsMillis: 60000}")
    private String timeBetweenEvictionRunsMillis;

    @Value("${spring.datasource.minEvictableIdleTimeMillis: 300000}")
    private String minEvictableIdleTimeMillis;

    @Value("${spring.datasource.validationQuery}")
    private String validationQuery;

    @Value("${spring.datasource.testWhileIdle}")
    private String testWhileIdle;

    @Value("${spring.datasource.testOnBorrow}")
    private String testOnBorrow;

    @Value("${spring.datasource.testOnReturn}")
    private String testOnReturn;

    @Value("${spring.datasource.poolPreparedStatements}")
    private String poolPreparedStatements;

    @Value("${spring.datasource.filters}")
    private String filters;

    @Bean
    public ServletRegistrationBean druidServlet() {
        ServletRegistrationBean reg = new ServletRegistrationBean();
        reg.setServlet(new StatViewServlet());
        reg.addUrlMappings("/druid/*");
        reg.addInitParameter("loginUsername", "hmkj");
        reg.addInitParameter("loginPassword", "hmkj2017");
        return reg;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
        filterRegistrationBean.addInitParameter("profileEnable", "true");
        filterRegistrationBean.addInitParameter("principalCookieName", "USER_COOKIE");
        filterRegistrationBean.addInitParameter("principalSessionName", "USER_SESSION");
        return filterRegistrationBean;
    }

    @Bean
    @Primary
    public DataSource druidDataSource(){
       System.out.println("=====================================");
        System.out.println("数据源注入");
        System.out.println("dbUrl="+dbUrl);
        System.out.println("driverClass="+driverClassName);
        System.out.println("=====================================");
        DruidDataSource datasource = new DruidDataSource();
        datasource.setUrl(this.dbUrl);
        datasource.setUsername(username);
        datasource.setPassword(password);
        datasource.setDriverClassName(driverClassName);
        datasource.setInitialSize(initialSize==null?1:Integer.parseInt(initialSize.trim()));
        datasource.setMinIdle( minIdle==null?1:Integer.parseInt(minIdle.trim()));
        datasource.setMaxActive(  maxActive==null?20:Integer.parseInt(maxActive.trim()));
        datasource.setMaxWait( maxWait==null?60000:Integer.parseInt(maxWait.trim()));
        datasource.setTimeBetweenEvictionRunsMillis( timeBetweenEvictionRunsMillis==null?60000:Integer.parseInt(timeBetweenEvictionRunsMillis.trim()));
        datasource.setMinEvictableIdleTimeMillis(  minEvictableIdleTimeMillis==null?300000:Integer.parseInt(minEvictableIdleTimeMillis.trim()));
        datasource.setValidationQuery(validationQuery);
        datasource.setTestWhileIdle(testWhileIdle==null?true:Boolean.valueOf(testWhileIdle.trim()));
        datasource.setTestOnBorrow(testOnBorrow==null?false:Boolean.valueOf(testOnBorrow.trim()));
        datasource.setTestOnReturn(testOnReturn==null?false:Boolean.valueOf(testOnReturn.trim()));
        datasource.setPoolPreparedStatements(poolPreparedStatements==null?true:Boolean.valueOf(poolPreparedStatements.trim()));

        try {
            datasource.setFilters(filters);
        } catch (SQLException e) {

        }
        return datasource;
    }


}
